package com.pignest.hotel.ui.reception;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.RoomRepairStatus;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.ui.room.RoomHistogram;
import com.pignest.hotel.ui.room.RoomView;
import com.pignest.hotel.util.DateUtil;

public class BookingHistogram extends RoomHistogram {
	private static final long serialVersionUID = 6002348821960125969L;
	
	private Date bookingTime;
	
	public BookingHistogram() {
		super();
		setShowFlags(RoomView.SHOW_STATUS | RoomView.SHOW_FEATURE | RoomView.SHOW_SELF_SERVICE | RoomView.SHOW_LIVING | RoomView.SHOW_BOOKING | RoomView.SHOW_ROOM_TYPE);
	}
	
	@Override
	protected List<Room> getRooms() {
		setLineCapacity(Config.getLineCapacityOfBooking());
		if (bookingTime == null) {
			RoomService roomService = Services.getRoomService();
			return roomService.listBookingRooms();
		}
		List<Living> livings = Services.getReceptionService().listCurrentLivings();
		HashSet<String> livingRooms = new HashSet<String>();
		for (Living living : livings) {
			if (living.getExpectLeaveDate() != null)
				if (living.getExpectLeaveDate().getTime() > bookingTime.getTime())
					livingRooms.add(living.getRoom().getName());
		}
		
		ArrayList<Room> r = new ArrayList<Room>();
		List<Room> rooms = Services.getRoomService().listAllRooms();
		for (Room room : rooms) {
			if (!RoomRepairStatus.NORMAL.is(room))
				continue;
			if (livingRooms.contains(room.getName()))
				continue;
			r.add(room);
		}
		return r;
	}
	
	public void setBookingTime(Date date) {
		if (date != null && date.getTime() < System.currentTimeMillis())
			date = null;
		this.bookingTime = date;
		refresh();
	}
	
	public void setBookingTime(Date date, String time) {
		if (date != null)
			date = DateUtil.parseDate(date, time);
		setBookingTime(date);
	}
}
